{literal}
<h2>Queries</h2>

To start a query you have to call one of these:<br>
1) YourClass::query() - query that returns objects<br>
2) YourClass::dataQuery() - query that returns arrays. (Basicly database rows. Faster than object query. You should use this if you don't need objects.)<br>
3) YourClass::idSetQuery( $column ) - query that returns array like this: array(ID=>value, ID=>value...) where value is $column value.<br>
<br>
All <b>DomArQueries</b> extend from <b>DomSqlQuery</b>.<br>
<br>
<h2>Object query</h2>
<pre class="code">
&lt;?php
// simple query with basic SQL components.
// This query returns Person objects
$persons = Person::query()
	->where('t.name LIKE \'Phil\'')
	->orderBy('t.birthday')
	->limit(50)
	->commit();
	
foreach( $persons as $person ) {
	echo $person->name;
}
	
// Joining tables. Notice that no rows or values are loaded from cars table. It is only used for ordering!	
$persons = Person::query()
	->select('t.name, t.age')
	->leftJoin('cars','c','c.driver = t.id')
	->where('t.name LIKE \'Phil\'')
	->orderBy('c.price')
	->limit(50)
	->commit();
	
foreach( $persons as $person ) {
	echo $person->name;
}
?&gt;
</pre>







<h2>Fast object joins</h2>

Some other methods of interest:<br />
<b>setClassName((string)$className)</b> - rarely used method. Needed if you use joinProperty and build query from DomArQuery (Not using ClassName::query()). <br />
<b>joinProperty</b> - fast way to load multiple objects into memory. <br />
<b>leftJoinProperty</b> - same as joinProperty but uses leftJoin<br /> 
<b>loadsMultipleObjects($bool)</b> - sets if this query returns data from different tables (which contain objects from different class). If it does, all these objects will be loaded. Result will look something like array(0=>array('t' => Object, 'x' = >Object)), where t and x are table aliases.<br />
<b>setMainObjectAlias((string)$alias)</b> - this table data (object) will be returned when iterated over results. If not set all objects are returned in an array where key is table alias.<br />
<b>setIndexColumnName((string)$columnName)</b> - Result array ket will not be counting from 0, but values of a column given by $columnName. (Be sure to use only unique columns!)<br />
<b>setValueColumnName((string)$columnName)</b> - Result array will not be 2 dimensional, but 1. For exaimple if column name would be "name", result might look like this: array(0 => 'Mary', 1 => 'Jana',...)<br />

<br>
Usage sample:<br>
<br>
<pre class="code">
&lt;?php
// lets consider we have Car and Person relationship where each car has one owner.
// Now lets load all cars.

$cars = Car::query()->commit()->toArray();
// Now we have array containing all cars.
foreach( $cars as $car ) {
	// this is slow since it uses lazyloading, it needs to send query to database for each car to find its owner.
	echo $car->owner->name;
}

// Use this one:
$cars = Car::query()->joinProperty('owner','o')->commit()->toArray();
// Now all Owner objects are automatically loaded and no need to create new queries.
foreach( $cars as $car ) {
	// This now does not send new query to database.
	echo $car->owner->name;
}

// When you need to use setClassName
$cars = DomArQuery::c()
	->from('cars','c')
	->setClassName('Car')
	->joinProperty('owner','o')
	->commit()
	->toArray();
// Now all Owner objects are automatically loaded and no need to create new queries.
foreach( $cars as $car ) {
	// This now does not send new query to database.
	echo $car->owner->name;
}


// previous case written out in a long way
$cars = DomArQuery::c()
	->from('cars','c')
	->join('owner','o','c.owner  = o.id')
	->loadsMultipleObjects(true)
	->setMainObjectAlias('c')
	->commit()
	->toArray();


// return both objects in a result seperately
$cars = DomArQuery::c()
	->from('cars','c')
	->join('owner','o','c.owner  = o.id')
	->loadsMultipleObjects(true)
	->setMainObjectAlias(NULL)
	->commit()
	->toArray();
	
foreach( $cars as $dat ) {
	echo $dat['c']->name.' is owned by '.$dat['o']->name."\n";
}


// Lets consider case where we have Car and people inside that car. (1:N relationship)
// persons.position says where each person is sitting and for driver persons.position=driver.
// We want to select all cars and their drivers fast.
// Use this one:
$cars = Car::query()
	->join('persons','d','d.car = t.id AND d.position=\'driver\'')
	->loadsMultipleObjects(true)
	->setMainObjectAlias(NULL)
	->commit()
	->toArray();

foreach( $cars as $dat ) {
	$car = $dat['c'];
	$driver = $dat['d'];
	echo $car->name.' is driver by '.$driver->name."\n";
}

// previous query in a longer way
$cars = DomArQuery::c()
	->from('cars','t')
	->join('persons','d','d.car = t.id AND d.position=\'driver\'')
	->loadsMultipleObjects(true)
	->setMainObjectAlias(NULL)
	->commit()
	->toArray();

?&gt;
</pre>


<h2>Data queries</h2>
<pre class="code">
&lt;?php
// Same as up, but returns array-s with data from database.
$persons = Person::dataQuery()
	->select('t.name, t.age')
	->where('t.name LIKE \'Phil\'')
	->orderBy('t.birthday')
	->limit(50)
	->commit();
	
foreach( $persons as $person ) {
	echo $person['name].' '.$person['age'];
}
	
// with dataqueries you can also make joins.
$persons = Person::dataQuery()
	->select('t.name, t.age, c.name as carName')
	->leftJoin('cars','c','c.driver = t.id')
	->where('t.name LIKE \'Phil\'')
	->orderBy('c.price')
	->limit(50)
	->commit();

foreach( $persons as $person ) {
	echo $person['name].' '.$person['carName'];
}

// Query that returns ID-s as keys and $value as values.
// This is very useful for example loading data for html selects
$personsList = Person::idSetQuery( 'name' )->orderBy('t.name')->commit();
foreach( $personsList as $id => $name ) {
	echo $id.' '.$name;
}
?&gt;
</pre>


<h2>Writing custom reusable queries and iterators</h2>

When doing this <b>be careful</b> to still manage <b>table indexes correctly</b>. <br />
This approach lets you create very powerful query engines, but you probably can't make that many indexes, so when starting to make cool query objects keep this in mind.
<pre class="code">
&lt;?php
class MyQuery extends DomArDataQuery() {

	public static function c() {
		return new MyQuery();
	}
	
	public function __construct() {
		parent::__construct();
		$this->from('myItems','t');
		$this->select('t.id, t.name, t.price');
	}

	public function getIterator() {
		return new MyIterator( $this );
	}
	
	/**
	* Our custom method that can be used. 
	* If you have many places that require this query with these two criterias you can write them only to one place.
	*
	* Also for this to be fast you should have index(active,expires)
	*/
	public function onlyPublic() {
		$this->where('t.active=1 AND t.expires&lt;NOW()');
	}
}

class MyIterator extends DomArDataIterator() {

	public static function c( DomArDataQuery $query, $alias = DomSql::DEFAULT_ALIAS ) {
		return new MyIterator( $query, $alias );
	}
	
	public function __construct( DomArDataQuery $query, $alias = DomSql::DEFAULT_ALIAS ) {
		parent::__construct( $query, $alias );
	}
	
	/**
	* After retrieving each row from database this method is called. $this->current is row that was just retrieved.
	*/
	public function parseCurrent() {
		$this->current['priceStr'] = number_format($this->current['price'],2,' ','.');
	}
}

// USING
// onlyPublic where parts are used on index, but t.id>10 and orderBy are not, so this is a slow query.
$result = MyQuery::c()->onlyPublic()->where('t.id>10')->orderBy('t.name')->limit(10)->commit();
foreach( $result as $row ) {
	echo $row['name'].' price: '.$row['priceStr'].'&lt;br&gt;';
}
// prints something like
Phil price: 10.00
Margus price: 123.45
...


?&gt;
</pre>
{/literal}